/*
 * @Author: soso
 * @Date: 2022-02-08 17:18:19
 * @LastEditTime: 2022-02-17 11:39:45
 * @LastEditors: Please set LastEditors
 * @Description: 正在处理中命令序列的磁盘同步处理
 * @FilePath: /sync-client/notify/ntfs.go
 */

package notify

import (
	"gomeshcli/sconfig"
	"io/ioutil"
	"os"

	"github.com/vmihailenco/msgpack"
)

// 保存正在处理的命令
func (nf *Ntfs) SaveRunningFile() error {
	if len(nf.runningNtfs) == 0 {
		_, err := os.Stat(pathOfRunning)
		if os.IsExist(err) {
			return nf.removeRf()
		}
	} else {
		return nf.saveRf()
	}
	return nil
}

func (nf *Ntfs) saveRf() error {
	data, _ := msgpack.Marshal(nf.runningNtfs)
	return ioutil.WriteFile(pathOfRunning, data, os.ModePerm)
}

func (nf *Ntfs) removeRf() error {
	return os.Remove(pathOfRunning)
}

// 加载正在处理中的命令
func (nf *Ntfs) loadRunningFile() error {
	nf.runningNtfs = []*sconfig.Snotify{}
	data, err := ioutil.ReadFile(pathOfRunning)
	if err != nil {
		return err
	}
	if len(data) > 0 {
		return msgpack.Unmarshal(data, &nf.runningNtfs)
	}
	return nil
}
